import akshare as ak
from clickhouse_driver import Client
from datetime import date, timedelta
import pandas as pd

CH = Client(host='clickhouse', port=9000, database='qbot')

def get_bars(codes: list[str], start: date, end: date):
    df_list = []
    for code in codes:
        df = ak.stock_zh_a_hist(symbol=code, period='daily', start_date=start.strftime('%Y%m%d'), end_date=end.strftime('%Y%m%d'), adjust='qfq')
        if df.empty: continue
        df = df[['日期', '开盘', '最高', '最低', '收盘', '成交量']]
        df.columns = ['date', 'open', 'high', 'low', 'close', 'vol']
        df['code'] = code
        df['date'] = pd.to_datetime(df['date'])
        df['adj_close'] = df['close'] * 0.999      # 简化滑点
        df_list.append(df)
    return pd.concat(df_list) if df_list else pd.DataFrame()
